﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace DroneLib
{
    internal static class Extensions
    {
        public static T[] MakeSize<T>(this T[] buffer, int length)
        {
            if (buffer.Length == length)
                return buffer;
            Array.Resize(ref buffer, length);
            return buffer;
        }
    }

    public static class CollectionExtensions
    {
        /// <summary>
        /// Performs the specified <paramref name="action"/> on each element of the <paramref name="enumerable"/>.
        /// 
        /// </summary>
        /// <param name="enumerable">An enumerable instance.
        ///             </param><param name="action"/>
        public static void ForEach(this IEnumerable enumerable, Action<object> action)
        {
            foreach (object obj in enumerable)
                action(obj);
        }

        /// <summary>
        /// Performs the specified <paramref name="action"/> on each element of the <paramref name="enumerable"/>.
        /// 
        /// </summary>
        /// <param name="enumerable">An enumerable instance.
        ///             </param><param name="action"/>
        public static void ForEach<T>(this IEnumerable enumerable, Action<T> action)
        {
            foreach (T obj in enumerable)
                action(obj);
        }

        /// <summary>
        /// Performs the specified <paramref name="action"/> on each element of the <paramref name="enumerable"/>.
        /// 
        /// </summary>
        /// <typeparam name="T">The type contained in the <paramref name="enumerable"/>.
        ///             </typeparam><param name="enumerable"/><param name="action"/>
        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            foreach (T obj in enumerable)
                action(obj);
        }

        public static string ToJSON(this object msg, Formatting fmt)
        {
            return JsonConvert.SerializeObject(msg, fmt, new JsonSerializerSettings()
            {
                Error =
                    (sender, args) => { args.ErrorContext.Handled = true; }
            });
        }

        public static string ToJSON(this object msg)
        {
            return msg.ToJSON(Formatting.Indented);
        }
    }
}
